<?php

namespace App\Http\Controllers\Admin;

use App\Http\Classes\AudioUpload;
use App\Http\Classes\Book;
use App\Http\Classes\Lesson;
use App\Http\Classes\Sentence;
use App\Http\Classes\Word;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Classes\Material;

class SentenceController extends Controller
{
    //
    public function viewSentenceHub()
    {

    }

    public function viewAddSentence()
    {
        try
        {
            $material = new Material();
            $materials = $material->getMaterials();
            if ( $materials )
            {
                $book = new Book();
                $books = $book->getBooksByMaterialId( $materials[0]->id );
                if ($books)
                {
                    $lesson = new Lesson();
                    $lessons = $lesson->getLessonsByBookId( $books[0]->id );
                    return view('admins.addSentence')->with('materials', $materials)
                        ->with('books', $books)->with('lessons', $lessons);
                }
                else
                {
                    return view('admins.addSentence')->with('materials', $materials)
                        ->with('books', $books)->with('lessons', null);
                }
            }
            else
            {
                return view('admins.addSentence')->with('materials', $materials)->with('books', null)->with('lessons', null);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionView($exception, __FUNCTION__);
        }
    }

    public function viewAddSentences()
    {
        try
        {
            $material = new Material();
            $materials = $material->getMaterials();
            if ( $materials )
            {
                $book = new Book();
                $books = $book->getBooksByMaterialId( $materials[0]->id );
                if ($books)
                {
                    $lesson = new Lesson();
                    $lessons = $lesson->getLessonsByBookId( $books[0]->id );
                    return view('admins.addSentences')->with('materials', $materials)
                        ->with('books', $books)->with('lessons', $lessons);
                }
                else
                {
                    return view('admins.addSentences')->with('materials', $materials)
                        ->with('books', $books)->with('lessons', null);
                }
            }
            else
            {
                return view('admins.addSentences')->with('materials', $materials)->with('books', null)->with('lessons', null);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionView($exception, __FUNCTION__);
        }
    }

    public function addSentences(Request $request)
    {
        try
        {
            $data = $request->all();
            $word_audios = new AudioUpload('word_audios', 1);
            $sentence_audios = new AudioUpload('sentence_audios', 2);
            $word_audios->setPath($data['material'], $data['book'], $data['lesson']);
            $sentence_audios->setPath($data['material'], $data['book'], $data['lesson']);

            if ( $word_audios->upload() )
            {
                $word_audios = $word_audios->getSavePaths();
            }
            else
            {
                return view('errors.500')->with('message', $word_audios->getErrorMessage());
            }

            if ( $sentence_audios->upload() )
            {
                $sentence_audios = $sentence_audios->getSavePaths();
            }
            else
            {
                return view('errors.500')->with('message', $sentence_audios->getErrorMessage());
            }

            $sentences = $data['sentences'];

            $lesson_id = $data['lesson'];

            $data = [];
            $words = [];

            $pattern_sentence = '/\.\s*?\[/i';
            $pattern_word = '/\[\w+\]/';
            $pattern_another = '/(?<=\[).+(?=\])/U';

            for( $i = 0; $i < count($sentences); $i++ )
            {
                $sentence = $sentences[$i];
                $sentence = preg_split($pattern_sentence, $sentence);
                $word = [];
                preg_match_all($pattern_word, $sentence[0], $word);
                $word = $word[0][0];
                $word = substr($word, 1, strlen($word) - 2);

                $sentence = str_replace('[','',$sentence[0]);
                $sentence = str_replace(']','',$sentence) . '.';

                $anothers = [];
                preg_match_all($pattern_another, $sentences[$i], $anothers);

                //dd($anothers);

                $data[$i]['word'] = $word;
                $data[$i]['word_meaning'] = $anothers[0][2];
                $data[$i]['word_phonogram'] = $anothers[0][3];
                $data[$i]['word_audio'] = $word_audios[$i];
                $data[$i]['sentence'] = $sentence;
                $data[$i]['sentence_meaning'] = $anothers[0][1];
                $data[$i]['sentence_audio'] = $sentence_audios[$i];
                $data[$i]['lesson_id'] = $lesson_id;

                $words[$i]['word'] = $data[$i]['word'];
                $words[$i]['word_meaning'] = $data[$i]['word_meaning'];
                $words[$i]['word_audio'] = $data[$i]['word_audio'];
                $words[$i]['word_phonogram'] = $data[$i]['word_phonogram'];
                $words[$i]['sentence'] = $data[$i]['sentence'];
                $words[$i]['sentence_meaning'] = $data[$i]['sentence_meaning'];
                $words[$i]['sentence_audio'] = $data[$i]['sentence_audio'];
                $words[$i]['lesson_id'] = $data[$i]['lesson_id'];
            }

            $sentence = new Sentence();
            $sentences = $sentence->addSentences($data);
            if ( ! $sentences )
            {
                return view('errors.500')->with('message', $sentence->getMessage());
            }

            $word = new Word();
            $words = $word->addWords($words);
            if ( ! $words )
            {
                return view('errors.500')->with('message', $word->getMessage());
            }

            return redirect('/admin/home');
        }
        catch (\Exception $exception)
        {
            return $this->exceptionView($exception, __FUNCTION__, '请按规定的格式录入');
        }
    }

    public function addSentence(Request $request)
    {
        try
        {
            $data = $request->all();
            $lesson_id = $data['lesson'];
            $dataArray = [];
            $dataArray['lesson_id'] = $lesson_id;
//            dd($data);
            $sentence_audio = new AudioUpload('sentence_audio', 2);
            $sentence_audio->setPath($data['material'], $data['book'], $data['lesson']);

            if ( $sentence_audio->upload() )
            {
                $sentence_audio = $sentence_audio->getSavePaths();

                $dataArray['sentence_audio'] = $sentence_audio[0];
            }

            if ( isset($data['word_audio']) )
            {
                $word_audio = new AudioUpload('word_audio', 1);
                $word_audio->setPath($data['material'], $data['book'], $data['lesson']);

                if ( $word_audio->upload() )
                {
                    $word_audio = $word_audio->getSavePaths();
                    $dataArray['word_audio'] = $word_audio[0];
                }
            }

            if ( ! empty($data['word']) )
            {
                $dataArray['word'] = $data['word'];
            }

            if ( ! empty($data['word_phonogram']) )
            {
                $dataArray['word_phonogram'] = $data['word_phonogram'];
            }

            if ( ! empty($data['word_meaning']) )
            {
                $dataArray['word_meaning'] = $data['word_meaning'];
            }

            $dataArray['sentence'] = $data['sentence'];
            $dataArray['sentence_meaning'] = $data['sentence_meaning'];

            $sentence = new Sentence();
            $sentences = $sentence->addSentences($dataArray);
            if ( ! $sentences )
            {
                return view('errors.500')->with('message', $sentence->getMessage());
            }

            if ( isset($dataArray['word']) )
            {
                $word = new Word();
                $words = $word->addWords($dataArray);
                if ( ! $words )
                {
                    return view('errors.500')->with('message', $word->getMessage());
                }
            }

            return redirect('/admin/home');
        }
        catch (\Exception $exception)
        {
            return $this->exceptionView($exception, __FUNCTION__, '请按规定的格式录入');
        }
    }
}
